package me.jessyan.armscomponent.commonsdk.utils;

import android.text.TextUtils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import me.jessyan.armscomponent.commonsdk.core.Constants;

/**
 * Desc:
 * Created by taoze
 * on 2021/9/19
 **/
public class SignUtil {

    public static String getSha1(String str) {
        if (null == str || 0 == str.length()) {
            return null;
        }
        char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'a', 'b', 'c', 'd', 'e', 'f'};
        try {
            MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
            mdTemp.update(str.getBytes("UTF-8"));

            byte[] md = mdTemp.digest();
            int j = md.length;
            char[] buf = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
                buf[k++] = hexDigits[byte0 & 0xf];
            }
            String result = new String(buf);
            return result.toUpperCase();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static String getSignData(String token,String uid){
        String result = "";
        long  timestrap = System.currentTimeMillis();
        String timestrapStr = timestrap + "";
        timestrapStr = timestrapStr.substring(0,10);
        if(TextUtils.isEmpty(token)){
            result = Constants.APP_KEY_NAME+ Constants.APP_KEY+ Constants.APP_SECRET_NAME+ Constants.APP_SECRET+ Constants.APP_TIMESTAMP_NAME + timestrapStr;
        }else{
            result = Constants.APP_KEY_NAME+ Constants.APP_KEY+ Constants.APP_SECRET_NAME+ Constants.APP_SECRET+ Constants.APP_TIMESTAMP_NAME + timestrapStr+ Constants.APP_TOKEN_NAME + token +Constants.APP_UID_NAME + uid;
        }

        return result;
    }

    public static String getSign(String token,String uid) {
        String str = getSignData(token, uid);
        if (null == str || 0 == str.length()) {
            return null;
        }
        char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'a', 'b', 'c', 'd', 'e', 'f'};
        try {
            MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
            mdTemp.update(str.getBytes("UTF-8"));

            byte[] md = mdTemp.digest();
            int j = md.length;
            char[] buf = new char[j * 2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
                buf[k++] = hexDigits[byte0 & 0xf];
            }
            String result = new String(buf);
            return result.toUpperCase();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

}
